package com.bj58.data.emailreports.sends;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.bj58.data.emailreports.controllers.KWDataController;
import com.bj58.data.emailreports.dao.SrcDbPool.DBType;
import com.bj58.data.emailreports.model.DataSourceCheck;
import com.bj58.data.emailreports.model.EmailReportUser;
import com.bj58.data.emailreports.utils.DateUtil;
import com.bj58.data.emailreports.utils.MailUtil;

public class KWDataSend extends BaseSend{

	public static void main(String[] args) {
		KWDataSend  sender = new KWDataSend();
		KWDataController  kwController = new KWDataController();
		MailUtil.initSCF();//SCF初始化
		try{
			String searchDate = DateUtil.getStringDate(new Date(), "yyyy-MM-dd");//查询日期
			if(null != args && args.length > 0){
				if(args.length != 1){
					System.out.println("params error");
					System.exit(0);
				}
				if(args[0].contains("-")){
					searchDate = args[0].trim();

				}
			}
			System.out.println(searchDate);
			
			String checkStartDate = DateUtil.getStringDate(DateUtil.getDateBefore(DateUtil.strToDate(searchDate, "yyyy-MM-dd"), 7),"yyyy-MM-dd");
			String checkEndDate = DateUtil.getStringDate(DateUtil.getDateBefore(DateUtil.strToDate(searchDate, "yyyy-MM-dd"), 1),"yyyy-MM-dd");
			
			String title = "搜索关键词数据报告 " + checkStartDate + " to " + checkEndDate;
			
			// 搜索关键词周报
			String types[] = { "黄页", "招聘", "房产", "二手市场", "二手车", "其他" };
			
			
			//数据校验
			for (String busiType : types) {
				List<DataSourceCheck> checkList =  sender.getCheckSourceList(checkStartDate, checkEndDate, busiType);
				for (DataSourceCheck checkVo : checkList) {
					if(!sender.checkDataSource(checkVo)){
						sender.errorWarn("搜索关键词周报 数据源 -"+checkVo.getTableName()+" 校验失败 !");
						System.exit(0);				}
				}
			}
			
			//收件人及发送
			int[] sendtype={21,22,23,24,25,27};
			for (int i = 1; i <= 6; i++) {
				String contextCpc = kwController.searchKeyWordWeekReport(searchDate, i);
				if ("".equals(contextCpc)) {
					sender.errorWarn(types[i-1] + title);
				} else {
					List<EmailReportUser> userList = sender.getUsersBySendType(sendtype[i-1]);
					sender.ReportSend(userList, types[i-1] + title,contextCpc, null);
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
			String content = "搜索关键词周报 发送异常!~~!!!~~  "+ DateUtil.getStringDate(new Date(), "yyyy-MM-dd");
			sender.errorWarn(content);
		}
	}
	
	
	/**获取校验数据源
	 * @param checkStartDate
	 * @param checkEndDate
	 * @return
	 * @throws Exception
	 */
	private List<DataSourceCheck>  getCheckSourceList(String checkStartDate,String checkEndDate,String busiType)throws Exception{
		List<DataSourceCheck> checkList = new ArrayList<DataSourceCheck>();
		
		String table = "";
		if("黄页".equals(busiType)){
			table = "hy";
		}else if("招聘".equals(busiType)){
			table = "zp";
		}else if("房产".equals(busiType)){
			table = "fc";
		}else if("二手市场".equals(busiType)){
			table = "es";
		}else if("二手车".equals(busiType)){
			table = "qc";
		}else if("其他".equals(busiType)){
			table = "other";
		}
		
		
		DataSourceCheck checkVo1 = new DataSourceCheck();
		
		checkVo1.setTableName(table + "_user_search_count_*yyyyMM*");
		checkVo1.setCheckStartDate(checkStartDate);
		checkVo1.setCheckEndDate(checkEndDate);
		checkVo1.setDataColName("stat_date");
		//checkVo1.setDbid(DBType.infobright_58.getValue());
		checkVo1.setDbid(DBType.db_94_data_app.getValue());
		
		checkList.add(checkVo1);
		
		
		
		DataSourceCheck checkVo2 = new DataSourceCheck();
		
		checkVo2.setTableName(table + "_user_search_info_analysis_*yyyyMM*");
		checkVo2.setCheckStartDate(checkStartDate);
		checkVo2.setCheckEndDate(checkEndDate);
		checkVo2.setDataColName("stat_date");
		//checkVo2.setDbid(DBType.infobright_58.getValue());
		checkVo2.setDbid(DBType.db_94_data_app.getValue());
		
		checkList.add(checkVo2);
		
		return checkList;
	}
}
